﻿// -----------------------------------------------------------------------
// <copyright file="Histogram.cs" company="Image Processing Course Project">
// Created 20/10/2011
// </copyright>
// -----------------------------------------------------------------------
namespace ImageProcessing
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using ImageProcessing.Utilities;

    /// <summary>
    /// A Class for the preparation of the image pixel color arrays needed for histogram drawing.
    /// </summary>
    public class Histogram
    {
        /// <summary>
        /// Array carrying the frequency of each red value in the pixels of the image
        /// </summary>
        private int[] red = new int[256];

        /// <summary>
        /// Array carrying the frequency of each green value in the pixels of the image
        /// </summary>
        private int[] green = new int[256];

        /// <summary>
        /// Array carrying the frequency of each blue value in the pixels of the image
        /// </summary>
        private int[] blue = new int[256];

        /// <summary>
        /// Array carrying the frequency of each grey value in the pixels of the grey-scaled image
        /// </summary>
        private int[] grey = new int[256];

        /// <summary>
        /// Local Bitmap image for processing
        /// </summary>
        private Bitmap image;

        /// <summary>
        /// Initializes a new instance of the Histogram class
        /// </summary>
        /// <param name="img">Currently displayed image</param>
        public Histogram(Image img)
        {
            this.image = new Bitmap(img);
            this.PrepareArrays(this.image);
        }

        /// <summary>
        /// Prepares the red, green, blue and grey-scaled arrays of the image needed to draw the histogram
        /// </summary>
        /// <param name="image">Currently displayed image</param>
        public void PrepareArrays(Image image)
        {
            try
            {
                Bitmap img = new Bitmap(image);
                int tempR, tempG, tempB;
                for (int i = 0; i < img.Height; i++)
                {
                    for (int j = 0; j < img.Width; j++)
                    {
                        tempR = img.GetPixel(j, i).R;
                        tempG = img.GetPixel(j, i).G;
                        tempB = img.GetPixel(j, i).B;
                        this.red[tempR]++;
                        this.green[tempG]++;
                        this.blue[tempB]++;
                        this.grey[(tempB + tempG + tempR) / 3]++;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw;
            }
        }

        /// <summary>
        /// Get array of the frequency of the red values in the image
        /// </summary>
        /// <returns>Integer array of red values</returns>
        public int[] GetRedArray()
        {
            return this.red;
        }

        /// <summary>
        /// Get array of the frequency of the green values in the image
        /// </summary>
        /// <returns>Integer array of green values</returns>
        public int[] GetGreenArray()
        {
            return this.green;
        }

        /// <summary>
        /// Get array of the frequency of the blue values in the image
        /// </summary>
        /// <returns>Integer array of blue values</returns>
        public int[] GetBlueArray()
        {
            return this.blue;
        }

        /// <summary>
        /// Get array of the frequency of the grey-scaled values in the image
        /// </summary>
        /// <returns>Integer array of grey-scaled values</returns>
        public int[] GetGreyArray()
        {
            return this.grey;
        }
    }
}
